package com.kaikeba.wechat.controller;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.mvc.ResponseView;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.UserSessionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {

    @ResponseView("/wx/createQRCode.do")
    public String createQRcode(HttpServletRequest req, HttpServletResponse resp){
        String type = req.getParameter("type");
        String code = req.getParameter("code");
        String qRCodeContent = null;

        if ("express".equals(type)){
            //单个快递二维码,放置取件二维码
            qRCodeContent = "QRCode_"+code;
        }else {
            //用户整体取件二维码，放置用户手机号码
            User user = UserSessionUtil.getUser(req.getSession());
            String phone = null;
            if (user!=null) {
                phone = user.getUserPhone();
            }else {
                Courier courier = UserSessionUtil.getCourier(req.getSession());
                if (courier!=null){
                    phone = courier.getUserPhone();
                }
            }
            qRCodeContent = "Phone_"+phone;
        }

        HttpSession session = req.getSession();
        session.setAttribute("qrcode",qRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String qRCode(HttpServletRequest req, HttpServletResponse resp){

        String qrcode = (String) req.getSession().getAttribute("qrcode");
        Message message = new Message();
        if (qrcode==null){
            message.setStatus(-1);
            message.setResult("取件码获取出错,请用户重新操作");
        }else {
            message.setStatus(0);
            message.setResult(qrcode);
        }
        return JsonUtil.toJson(message);

    }



}
